package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import entity.Goods;
import entity.OrderItem;
import entity.UserAddress;
import tools.DBTools;

public class UserAddressDao {
	//获取
	public List<UserAddress> getUserAddressesById(int addressId){
		List<UserAddress> userAddresses = new ArrayList<>();
		 Connection conn = DBTools.getInstance().getConnection();
		String sql="select * from tbl_user_address  where address_id=?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps=conn.prepareStatement(sql);
			ps.setInt(1, addressId);
			rs=ps.executeQuery();
			while(rs.next())
			{
				UserAddress userAddress=new UserAddress();
				userAddress.setAddressId(rs.getInt(1));
				userAddress.setUserId(rs.getInt(2));
				userAddress.setDetailAddress(rs.getString(3));
				userAddress.setContactPhone(rs.getString(4));
				userAddress.setPostCode(rs.getString(5));
				userAddress.setAddressType(rs.getString(6));
				userAddresses.add(userAddress);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBTools.getInstance().closeConnection(conn, ps, rs);
		}
		return userAddresses;
	}
	
	//获取用户的地址根据用户id
	public UserAddress getUserAddressById(int addressId){
		UserAddress userAddress = new UserAddress();
		 Connection conn = DBTools.getInstance().getConnection();
		String sql="select * from tbl_user_address  where address_id=?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps=conn.prepareStatement(sql);
			ps.setInt(1, addressId);
			rs=ps.executeQuery();
			while(rs.next())
			{
				userAddress.setAddressId(rs.getInt(1));
				userAddress.setUserId(rs.getInt(2));
				userAddress.setDetailAddress(rs.getString(3));
				userAddress.setContactPhone(rs.getString(4));
				userAddress.setPostCode(rs.getString(5));
				userAddress.setAddressType(rs.getString(6));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBTools.getInstance().closeConnection(conn, ps, rs);
		}
		return userAddress;
	}
	
	//根据用户id获取他的所有的地址
	public List<UserAddress> getUserAllAddrByUserId(int userId){
		List<UserAddress> addressList = new ArrayList<>();
		 Connection conn = DBTools.getInstance().getConnection();
		String sql="select * from tbl_user_address  where id=?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps=conn.prepareStatement(sql);
			ps.setInt(1, userId);
			rs=ps.executeQuery();
			while(rs.next())
			{
				UserAddress userAddress=new UserAddress();
				userAddress.setAddressId(rs.getInt(1));
				userAddress.setUserId(rs.getInt(2));
				userAddress.setDetailAddress(rs.getString(3));
				userAddress.setContactPhone(rs.getString(4));
				userAddress.setPostCode(rs.getString(5));
				userAddress.setAddressType(rs.getString(6));
				addressList.add(userAddress);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBTools.getInstance().closeConnection(conn, ps, rs);
		}
		return addressList;
	}
}
